<?php
class IndexAction extends Action {
    public function index(){
        if(session('?username')){

            import ( '@.ORG.Util.RBAC' );
            RBAC::saveAccessList();

            $admin_db = new Model('admin');
            $condition_admin['adminid']=session('userid');
            $local_admin = $admin_db->field('*')->where($condition_admin)->find();

            $access = new Model('access');
            $menuid =$access->field('node_id')->where(array('role_id'=>$local_admin['roleid'],'level'=>1))->select();
            foreach($menuid as $val){
                $arr[]=$val['node_id'];
            }
            if(session('roleid')==1){
                $node = new Model('node');
                $title=$node->field('id,title,name')->where(array('level'=>1,'location'=>1,'status'=>1))->order('sort ASC')->select();
            }else{
                $node = new Model('node');
                $title=$node->field('id,title,name')->where(array('level'=>1,'location'=>1,'status'=>1,'id'=>array('in',$arr)))->order('sort ASC')->select();
            }

            foreach($title as $key=>$tval){
                $menutitle[]=$tval;
            }
            $this->assign('menutitle',$menutitle);

            $this->display('admin');
        }
        else{
            redirect(U('Login/index'));
        }
    }

    /**
     * lujinfa
     * 2013-09-02
     * 生在验证码
     */
    public function checkcode()
    {
        //导入验证码类
        import("@.ORG.Util.Image");
        Image::buildImageVerify();
    }
    //后台当前位置面包屑处理
    public function public_current_pos() {
        echo $this->current_pos($_GET['menuid']);
        exit;
    }
    public static function current_pos($id) {
        $node = new Model('node');
        $r =$node->field('id,title,pid')->where(array('id'=>$id))->find();
        $str = '';
        if($r['pid'] && $r['pid']!=1) {
            $str = self::current_pos($r['pid']);
        }
        return $str.$r['title'].' > ';
    }

    //左侧静态页
    public function left(){
        //echo "<Br>----------------------->";
        //var_dump($_SESSION);
        if(session('userid') !=null){

            $menuid = !empty($_REQUEST['menuid'])?trim($_REQUEST['menuid']):2;

            $admin_db = new Model('admin');
            $condition_admin['adminid']=session('userid');
            $local_admin = $admin_db->field('*')->where($condition_admin)->find();

			$access = new Model('access');
			$leftbig = $access->field('node_id')->where(array('role_id'=>$local_admin['roleid'],'pid'=>$menuid))->select();
			foreach($leftbig as $bigval){
				$bigarr[]=$bigval['node_id'];
			}

			$node=new Model('node');
			if(session('username')=='admin'){
				$nodemg=$node->field('id,title,name,g,m,a,data')->where(array('pid'=>$menuid,'status'=>1))->select();
			}else{
				$nodemg=$node->field('id,title,name,g,m,a,data')->where(array('id'=>array('in',$bigarr)))->select();
			}

			foreach($nodemg as $nodeval){
				$leftsmall = $access->field('node_id')->where(array('role_id'=>$local_admin['roleid'],'pid'=>$nodeval['id']))->select();
				 $smallarr=array();
				foreach($leftsmall as $smallval){
				$smallarr[]=$smallval['node_id'];
				}
				if(session('username')=='admin'){
					$nodemsg=$node->field('id,title,name,g,m,a,data')->where(array('pid'=>$nodeval['id'],'level'=>3,'status'=>1))->order('id ASC')->select();
					if(empty($nodemsg)){
						$nodepmsg=$node->field('id')->where(array('pid'=>$nodeval['id'],'status'=>1))->order('id ASC')->select();
						$pmsgarr=array();
						foreach($nodepmsg as $pmsgval){
							$pmsgarr[]=$pmsgval['id'];
						}
						$nodemsg=$node->field('id,title,name,g,m,a,data')->where(array('pid'=>array('in',$pmsgarr),'level'=>3,'status'=>1))->order('id ASC')->select();
					}
				}else{
					$nodemsg=$node->field('id,title,name,g,m,a,data')->where(array('id'=>array('in',$smallarr),'level'=>3,'status'=>1))->order('id ASC')->select();
					if(empty($nodemsg)){
					$nodemsg=$node->field('id,title,name,g,m,a,data')->where(array('pid'=>array('in',$smallarr),'level'=>3,'status'=>1))->order('id ASC')->select();
					}
				}

				$smlarr=array();
				foreach($nodemsg as $smlval){
					$smlarr[]=$smlval;
				}
				$nodearr=array();
				$nodearr['title']= $nodeval['title'];
				$nodearr['child']=$smlarr;

				$newnode[]=$nodearr;
			}
			$this->assign('nodearr',$newnode);
			$this->display('left');
        }else{

            echo "<Br>---------2222222222222222222-------------->";
            $this->error('请先登录','__APP__/Index/index');
        }
    }
}